<template>
  <!-- 配菜组件 -->
  <div class="side-dishes">
    <!-- 总内容 -->
    <div class="foods-wrapper">
      <!-- 菜品图片 -->
      <div class="food-box">
        <div class="icon">
          <img :src="food.img"
               width="90"
               height="90" />
        </div>
        <div class="content">
          <!-- 菜品名字 -->
          <div class="name">
            {{food.name}}
          </div>
          <!-- 菜品价格 -->
          <div class="price">
            <span class="now">{{currencyDefault}} {{comboPrice}}</span>
          </div>
          <div>{{selectedOptions}}</div>
        </div>
        <div @click="$emit('close')">
          <img src="../../../static/imgs/close.png"
               width="32"
               height="32">
        </div>
      </div>
      <!-- 配菜标题 -->
      <div class="setmealcombo"
           ref='setmeal_Combo'
           style="height:150px;overflow: hidden">
        <div>
          <div class="spec-box"
               v-for="(option, optionIndex) in food.goods_spec"
               :key="option.id">
            <div class="spec-title">{{option.title}}</div>
            <!-- 配菜选项 -->
            <ul class="spec-wrapper">
              <li :class="{'current': selected[optionIndex] && selected[optionIndex].id === item.id}"
                  v-for="item in option.goods_spec_item"
                  :key="item.id"
                  @click="selectSpec(item, optionIndex)">{{item.title}}
              </li>
            </ul>
          </div>
        </div>
      </div>
      <!-- 确定按钮 -->
      <div class="spec-btn"
           @click="confirmSpec">{{$t('message.okBtn')}}</div>
    </div>
  </div>
</template>

<script>

import BScroll from 'better-scroll'
export default {
  name: 'specifications',
  data () {
    return {
      selected: []
    }
  },
  props: {
    currencyDefault: {
      type: String
    },
    food: {
      type: Object
    },
    cartFoods: {
      type: Array
    }
  },
  created () {
    //  默认选择
    this.selected = this.food.goods_spec.map(item => item.goods_spec_item[0])
  },
  methods: {
    //  选择
    selectSpec (item, index) {
      this.selected.splice(index, 1, item)
    },
    //  确定
    confirmSpec () {
      const selectedOptions = this.selected.map(item => item.title).join(',')
      let foundFood = this.cartFoods.find(({ id, options }) => id === this.food.id && options === selectedOptions)
      if (!foundFood) {
        let foodCopy = JSON.parse(JSON.stringify(this.food))
        foodCopy.acount = 1
        foodCopy.options = selectedOptions
        foodCopy.cost = this.comboPrice
        this.$emit('add', foodCopy)
      } else {
        foundFood.acount += 1
        this.$emit('close')
      }
    }
  },
  computed: {
    //  已选
    selectedOptions () {
      if (this.selected.length > 0) {
        return this.selected.map(item => item.title).join(' / ')
      } else {
        return ''
      }
    },
    //  套餐价格
    comboPrice () {
      let sum = +this.food.cost
      this.selected.forEach(item => {
        sum += +item.add_price
      })
      return sum.toFixed(2)
    }
  

  },
  mounted () {
  

    this.$nextTick(() => {
      if (!this.scroll) {
      this.scroll = new BScroll(this.$refs.setmeal_Combo, {
        click: true
      })
      }
    })
    


  }

}
</script>
<style scoped lang="stylus">
.side-dishes {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 500;
  width: 100%;
  background: white;

  .foods-wrapper {
    width: 100%;
    height: 100%;
    background: #fff;
    overflow: hidden;

    .food-box {
      display: flex;
      padding: 10px;

      .icon {
        background-repeat: no-repeat;
      }

      .content {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        padding: 5px 10px;
        flex: 1;

        .name {
          font-size: 12px;
          color: rgb(7, 17, 27);
        }

        .price {
          font-weight: 700;
          line-height: 24px;

          .now {
            margin-right: 8px;
            font-size: 18px;
            color: rgb(240, 20, 20);
          }
        }
      }
    }

    .spec-box {
      padding: 10px;

      .spec-title {
        font-weight: 550;
      }

      .spec-wrapper {
        margin-top: 7px;
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        margin-bottom: -10px;

        li {
          margin-right: 15px;
          margin-bottom: 10px;
          text-align: center;
          /* display: inline-block; */
          height: 35px;
          line-height: 35px;
          padding: 0 14px;
          background-color: #f5f5f5;
          border-radius: 3px;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;

          &.current {
            color: #00a0dc;
            background-color: #e0f1ff;
          }
        }
      }
    }

    .spec-btn {
      text-align: center;
      margin: 20px auto;
      width: 343px;
      height: 40px;
      line-height: 40px;
      font-size: 16px;
      background-color: #00a0dc;
      -webkit-border-radius: 3px;
      -moz-border-radius: 3px;
      border-radius: 3px;
      color: white;
    }
  }
}
</style>
